package com.google.demo;

import java.util.HashMap;

import com.google.demo.Util.Prefs;
import com.google.demo.model.UserInfo;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;

public class ScanQRCodeApp extends Application {
	
	private final HashMap<Object, Object> _data = new HashMap<Object, Object>();
	private static ScanQRCodeApp _instance;
	public boolean isFirstStartLoginActivity = true;
	private boolean isLogin = false;
	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}
	public boolean isLogin() {
		return isLogin;
	}
	public ScanQRCodeApp() {
		// TODO Auto-generated constructor stub
		super();
		_instance = this;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
	}

	public static ScanQRCodeApp Instance() {
		// TODO Auto-generated method stub
		return _instance;
	}

	public String getAppID() {
		// TODO Auto-generated method stub
		if (getPackageName().equals("com.google.demo")) {
			return Config.AppID;
		} else
			return Config.AppID;
	}

	public String getAppVersion() {
		// TODO Auto-generated method stub
		String version;
		try {
			version = getPackageManager().getPackageInfo(
					getApplicationInfo().packageName, 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			version = "1.0.0";
		}
		return version;
	}

	public boolean isDebugBuild() {
		// TODO Auto-generated method stub
		boolean flag = false;
		PackageInfo pi;
		try {
			pi = getPackageManager().getPackageInfo(getPackageName(), 0);
			flag = ((pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public String getDeviceId() {
		return Settings.Secure.getString(getContentResolver(),
				Settings.Secure.ANDROID_ID);
	}
	public void putData(Object key, Object value) {
		_data.put(key, value);
	}

	public void removeData(Object key) {
		_data.remove(key);
	}

	public Object getData(Object key) {
		return _data.get(key);
	}
	public void saveLoginInfo(UserInfo userInfo) {
		Prefs prefs = new Prefs(getApplicationContext());
		prefs.putBooleanValue(true, Config.IS_LOGIN_TAG);
		prefs.putStringValue(userInfo.getmUsername(),
				Config.USERNAME);
		prefs.putStringValue(userInfo.getmDeviceId(),
				Config.DEVICEID);
		setLogin(true);
	}
	public boolean isFirstLaunch() {
		boolean isFirstLaunch = true;
		Prefs prefs = new Prefs(getApplicationContext());
		isFirstLaunch = prefs.getBooleanValue(true, Config.IS_FIRST_LAUNCH_TAG);
		prefs.putBooleanValue(false, Config.IS_FIRST_LAUNCH_TAG);
		return isFirstLaunch;
	}
	public UserInfo getLoginInfo() {
		UserInfo userInfo = new UserInfo();
		Prefs prefs = new Prefs(getApplicationContext());
		boolean isLogin = prefs.getBooleanValue(false,
				Config.IS_LOGIN_TAG);
		String userID = prefs.getStringValue("",
				Config.USERNAME);	
		String devid=prefs.getStringValue("", Config.DEVICEID);
		userInfo.setmIsLogin(isLogin);
		userInfo.setmUsername(userID);
		userInfo.setmDeviceId(devid);
		setLogin(isLogin);
		return userInfo;
	}
	public void saveLogoutInfo() {
		Prefs prefs = new Prefs(getApplicationContext());
		prefs.putBooleanValue(false, Config.IS_LOGIN_TAG);
		prefs.putStringValue("", Config.USERNAME);
		setLogin(false);
	}

	public boolean isLoginApp() {
		Prefs prefs = new Prefs(getApplicationContext());
		return prefs.getBooleanValue(false, Config.IS_LOGIN_TAG);
	}

}
